package inventory.service.impl;

import inventory.entity.Notice;
import inventory.entity.Role;
import inventory.entity.po.NoticePo;
import inventory.entity.po.ReparationPo;
import inventory.mappers.NoticeMapper;
import inventory.mappers.ReparationRecordMapper;
import inventory.mappers.UserMapper;
import inventory.service.FeedbackService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class FeedbackServiceImpl implements FeedbackService {
    @Resource
    NoticeMapper noticeMapper;
    @Resource
    ReparationRecordMapper reparationRecordMapper;

    @Resource
    UserMapper userMapper;


    @Override
    public int addNotice(String launcher, String noticeContext) {
        Notice notice = new Notice();
        notice.setNoticeId(UUID.randomUUID().toString().replace("-",""));
        notice.setLauncher(launcher);
        Role role = noticeMapper.queryRoleByLauncher(launcher);
        if (role.getState().equals("1")){
            notice.setState(role.getRole()+"公告");
        }else if(role.getState().equals("2")){
            notice.setState(role.getRole()+"公告");
        }
        notice.setNoticeContext(noticeContext);
        Date date = new Date();
        notice.setLaunchTime(date);
        int i = noticeMapper.insert(notice);
        return i;
    }


    @Override
    public int deleteNoticeById(String notice_id) {
        int i = noticeMapper.deleteById(notice_id);
        return i;
    }

//    @Override
//    public ResultData queryNotice() {
//
//        List<NoticePo> noticePos = noticeMapper.queryAllNotice();
////        for (NoticePo noticePo:notices){
////            User user = userMapper.selectById(noticePo.getLauncher());
////            if(user!=null) {
////                noticePo.setUsername(user.getUsername());
////            }
////        }
//        HashMap<String, Object> rs = new HashMap<>();
//        rs.put("count",noticePos.size());
//        rs.put("allNotices",noticePos);
//        /**
//         * mybatisplus的分页查询
//         */
////        ArrayList<String> strings = new ArrayList<>();
////        strings.add("launch_time");
////        List<Notice> notices = noticeMapper.selectPage(new Page<Notice>(2,2), new EntityWrapper<Notice>().orderDesc(strings));
//        return new ResultData().OK().setObjs(noticePos);
//    }

    @Override
    public List<NoticePo> selectAllNoticePo() {
        List<NoticePo> noticePos = noticeMapper.selectAllNoticePo();
        return noticePos;
    }
    @Override
    public void updateNoticeById(String noticeId, String noticeContext) {
        noticeMapper.updateNoticeById(noticeId,noticeContext);
    }

    @Override
    public List<NoticePo> selectNoticeByLauncherRoleId(String launcherRoleId) {
        System.out.println(launcherRoleId);
        List<NoticePo> notices = noticeMapper.selectNoticeByLauncherRoleId(launcherRoleId);
        return notices;
    }









    @Override
    public int insert(ReparationPo reparationPo){
        int i = reparationRecordMapper.insertReparationPo(reparationPo);
        return i;
    }

    @Override
    public List<ReparationPo> queryRepairByUserId(String userId) {
        Role role = reparationRecordMapper.selectUserRoleByUserId(userId);
        List<ReparationPo> reparationPos = null;
        if(role.getState().equals("0")){
            //查看底层员工
            reparationPos = reparationRecordMapper.selectRepairByUserId(userId);
        }else if (role.getState().equals("1")){
            reparationPos = reparationRecordMapper.selectRepairByCountyUserId(userId);
        }else if (role.getState().equals("2")){
             reparationPos = reparationRecordMapper.selectRepairByCityUserId(userId);
        }
        return reparationPos;
    }



    @Override
    public int deleteReparationPoByreparationId(String reparationId) {
        int i = reparationRecordMapper.deleteReparationPoByReparationId(reparationId);
        return i;
    }


    @Override
    public Role selectPidByReparationerId(String reparationerId) {
        Role role = reparationRecordMapper.selectUserPidByReparationerId(reparationerId);
        return role;
    }

    @Override
    public Role selectPidByRoleId(String roleId) {
        Role role = reparationRecordMapper.selectUserPidByRoleId(roleId);
        return role;
    }




    @Override
    public int updateReparationPoByreparationId(String reparationId,String userId,String decide) {
        Role role = reparationRecordMapper.selectUserPidByReparationerId(userId);
        String handleProgress="";
        System.out.println(role.getState());
        switch (role.getState()){
           case "1":{
               if (decide.equals("1")){
                   handleProgress = "3";
               }
               else {
                   handleProgress = "2";
               }
               break;
           }
           case "2":{
               if (decide.equals("1")){
                   handleProgress = "5";
               }
               else {
                   handleProgress = "4";
               }
               break;
           }

       }
        System.out.println(handleProgress);
        int i = reparationRecordMapper.updateReparationByreparationId(reparationId,userId,handleProgress);
        return i;
    }


}
